//
//  BKFeedBackViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/27.
//  Copyright (c) 2015年 报考大学. All rights reserved.
//

#import "BKFeedBackViewController.h"
#import "LoginViewController.h"
#import <MBProgressHUD.h>
#import "BKFeedBackViewModel.h"

@interface BKFeedBackViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *placeLabel;
@property (weak, nonatomic) IBOutlet UITextView *feedbackTextView;

@end

@implementation BKFeedBackViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [_feedbackTextView becomeFirstResponder];
    // Do any additional setup after loading the view from its nib.
    _feedbackTextView.layer.borderColor = [[UIColor colorWithRed:0.322 green:0.620 blue:0.902 alpha:1.000]CGColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)p_back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)p_submit:(id)sender {
    
    if ([_feedbackTextView.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入反馈内容" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    if ([_feedbackTextView.text length]>150) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"反馈内容不能大于150字" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        return;
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"Cookie"] !=nil) {
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:hud];
        [hud removeFromSuperViewOnHide];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.labelText = @"正在提交";
        [hud show:YES];
        
        [BKFeedBackViewModel feedBackWithContent:_feedbackTextView.text success:^{
            [hud hide:YES];
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alertView show];
        } failure:^(NSString *message) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
            [alertView show];
        }];
    
    }else{
    
        LoginViewController *login = [[LoginViewController alloc] initWithNibName:NSStringFromClass([LoginViewController class]) bundle:nil];
        [self presentViewController:login animated:YES completion:nil];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{

    _placeLabel.hidden = YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView{

    if ([textView.text length]>0) {
        _placeLabel.hidden = YES;
    }else{
    
        _placeLabel.hidden = NO;
    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_feedbackTextView resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
